package com.frxuexi.park2212.park

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.frxuexi.lib_base.base.BaseViewModel
import com.frxuexi.lib_base.entity.ParkBean
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class ParkViewModel @Inject constructor(val apiService: ApiService):BaseViewModel() {
    var addPark =MutableLiveData<ParkBeanItem>()

    var editPark =MutableLiveData<ParkBeanItem>()
    fun getParksByType(para:String,type:String):Flow<PagingData<ParkBeanItem>>{
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            ParkDataSource(apiService,para,type)
        }).flow.cachedIn(viewModelScope)
    }

    fun addPark(beanItem: ParkBeanItem){
        httpRequest({apiService.addPark(toBody(beanItem))},addPark::postValue)
    }

    fun editPark(id: Int,beanItem: ParkBeanItem){
        httpRequest({apiService.editPark(id,toBody(beanItem))},editPark::postValue)
    }
}